#include "editcommodity.h"
#include "ui_editcommodity.h"
#include <QDateTime>
#include <QMessageBox>
#include <QFileDialog>
#include <QSqlQuery>
#include <QTableWidgetItem>

EditCommodity::EditCommodity(QWidget *parent, const QString &stockId)
    : QDialog(parent)
    , ui(new Ui::EditCommodity)
    , m_stockId(stockId)
{
    ui->setupUi(this);
    // 设置窗口属性
    setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);
    setFixedSize(this->width(), this->height());

    // 设置商品ID为只读
    ui->stockIdEdit->setReadOnly(true);

    // 加载商品数据
    loadCommodityData();
}

EditCommodity::~EditCommodity()
{
    delete ui;
}

// 加载商品数据
void EditCommodity::loadCommodityData()
{
    if (m_stockId.isEmpty()) {
        QMessageBox::warning(this, "警告", "商品ID为空，无法加载数据");
        reject();
        return;
    }

    QSqlQuery query;
    query.prepare("SELECT StockId, StockName, StockAmount, StockUnitprice, "
                  "Supplier, Director, WareHousetime, DeliveryTime, Remarks "
                  "FROM commoditydatatable WHERE StockId = ?");
    query.addBindValue(m_stockId);

    if (query.exec() && query.next()) {
        // 填充表单数据
        ui->stockIdEdit->setText(query.value(0).toString());
        ui->stockNameEdit->setText(query.value(1).toString());
        ui->stockAmountEdit->setText(query.value(2).toString());
        ui->stockAmountEdit->setReadOnly(true);  // 库存数量只读，不允许修改
        ui->unitPriceEdit->setText(query.value(3).toString());
        ui->supplierEdit->setText(query.value(4).toString());
        ui->directorEdit->setText(query.value(5).toString());
        // ui->warehouseTimeEdit->setDateTime(query.value(6).toDateTime());
        // ui->deliveryTimeEdit->setDateTime(query.value(7).toDateTime());
        ui->remarksEdit->setPlainText(query.value(8).toString());
    } else {
        QMessageBox::warning(this, "错误",
                             QString("加载商品数据失败！错误信息：%1").arg(query.lastError().text()));
        reject();
    }
}

// 更新商品数据
bool EditCommodity::updateCommodityData()
{
    QSqlQuery query;
    query.prepare("UPDATE commoditydatatable SET "
                  "StockName = ?, StockUnitprice = ?, Supplier = ?, Director = ?, "
                  "Remarks = ? "
                  "WHERE StockId = ?");

    query.addBindValue(ui->stockNameEdit->text().trimmed());
    query.addBindValue(ui->unitPriceEdit->text().toDouble());
    query.addBindValue(ui->supplierEdit->text().trimmed());
    query.addBindValue(ui->directorEdit->text().trimmed());
    // query.addBindValue(ui->warehouseTimeEdit->dateTime());
    // query.addBindValue(ui->deliveryTimeEdit->dateTime());
    query.addBindValue(ui->remarksEdit->toPlainText().trimmed());
    query.addBindValue(m_stockId);

    if (query.exec()) {
        return true;
    } else {
        QMessageBox::warning(this, "错误",
                             QString("更新商品数据失败！错误信息：%1").arg(query.lastError().text()));
        return false;
    }
}

void EditCommodity::on_exitButton_clicked()
{
    accept();
}


void EditCommodity::on_addButton_clicked()
{
    // 验证必填字段
    if (ui->stockNameEdit->text().trimmed().isEmpty()) {
        QMessageBox::warning(this, "警告", "商品名称不能为空！");
        ui->stockNameEdit->setFocus();
        return;
    }

    if (ui->unitPriceEdit->text().trimmed().isEmpty()) {
        QMessageBox::warning(this, "警告", "商品单价不能为空！");
        ui->unitPriceEdit->setFocus();
        return;
    }

    // 验证单价是否为有效数字
    bool ok;
    double unitPrice = ui->unitPriceEdit->text().toDouble(&ok);
    if (!ok || unitPrice < 0) {
        QMessageBox::warning(this, "警告", "商品单价必须为有效的正数！");
        ui->unitPriceEdit->setFocus();
        ui->unitPriceEdit->selectAll();
        return;
    }

    if (updateCommodityData()) {
        QMessageBox::information(this, "提示", "商品信息修改成功！");
        accept();
    }
}

